import KoaRouter from 'koa-router'
import Config from '@/config/Config'
import { Models } from '@/common/typings/modle'
import { HttpException, Success } from '@/core/HttpException'
import validator from '@/middlewares/validator'
import schema from '@/common/api-json-schema/system/email/valid'
import { getSmsCode } from '@/server/redis'
const router = new KoaRouter({
  prefix: `${Config.API_PREFIX}v1/system/email`,
})
router.post('/valid', validator(schema, 'body'), async (ctx: Models.Ctx) => {
  const { email, validCode } = ctx.request.body as any
  const redisEmailCodeRes = await getSmsCode(email)
  const redisEmailCode = redisEmailCodeRes ? redisEmailCodeRes.results || '' : ''
  if (!redisEmailCode) {
    throw new HttpException({ msg: '验证码已过期，请重新获取' })
  }
  console.log('email:', email)
  console.log('validCode:', validCode)
  console.log('redisEmailCode:', redisEmailCode)
  if (validCode === redisEmailCode) {
    throw new Success()
  } else {
    throw new HttpException({ msg: '验证码错误！' })
    // throw new Success({errorCode: -1, msg: ''})
  }
})
export default router
